package com.llh.msg;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import weaver.rsa.security.RSA;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: 刘立华
 * @date: 2022-06-17 10:48
 */
public class TokenAndMsgService {
    //OA系统提供的appid
    private static String APPID = "bfa4eec5-f856-46ac-8d22-8670bb0586cd";
    //OA系统的服务地址
    private static String address = "https://sjoa.sjzszjjt.com";
    //
    private static String code = "1218";

    //测试方法
    public static void main(String[] args) {
        String regist = regist();
        System.out.println("regist返回结果 = " + regist);
        if (JSONUtil.isJsonObj(regist)) {
            JSONObject registObj = JSONUtil.parseObj(regist);
            String secrit = registObj.getStr("secrit");
            //公钥
            String spk = registObj.getStr("spk");
            RSA rsa = new RSA();
            // 对secrit进行加密
            String secret = rsa.encrypt(null, secrit, null, "utf-8", spk, false);
            if (secret != null) {
                String applyToken = applyToken(secret);
                System.out.println("获取token返回结果 = " + applyToken);
                if (JSONUtil.isJsonObj(applyToken)) {
                    JSONObject applyTokenObj = JSONUtil.parseObj(applyToken);
                    String token = applyTokenObj.getStr("token");
                    if (token != null) {
                        String msg = sendMsg(token);
                        System.out.println("sendMsg返回结果 = " + msg);
                    }
                }

            }
        }
    }

    /**
     * 注册
     *
     * @return
     */
    private static String regist() {
        Map<String, List<String>> headers = new HashMap<>();
        //随机生成一个公钥明文
        String cpk = new RSA().getRSA_PUB();
        List<String> appidlist = new ArrayList<>();
        appidlist.add(APPID);
        headers.put("appid", appidlist);
        List<String> cpklist = new ArrayList<>();
        cpklist.add(cpk);
        headers.put("cpk", cpklist);
        return post("/api/ec/dev/auth/regist", headers, null);
    }

    /**
     * 获取token
     *
     * @param secret
     * @return
     */
    private static String applyToken(String secret) {
        Map<String, List<String>> headers = new HashMap<>();

        //随机生成一个公钥明文
        String cpk = new RSA().getRSA_PUB();
        List<String> appidlist = new ArrayList<>();
        appidlist.add(APPID);
        headers.put("appid", appidlist);
        List<String> secretlist = new ArrayList<>();
        secretlist.add(secret);
        headers.put("secret", secretlist);
        //超时时间
        List<String> timelist = new ArrayList<>();
        //超时时间半小时
        timelist.add("1800");
        headers.put("time", timelist);
        return post("/api/ec/dev/auth/applytoken", headers, null);
    }

    /**
     * post请求
     *
     * @param path
     * @param headers
     * @param params
     * @return
     */
    private static String post(String path, Map<String, List<String>> headers, Map<String, String> params) {
        return HttpRequest.post(address + path)
                .header("accept", "*")
                .header("connection", "keep-alive")
                .header(headers, false)
                .body(mapToStr(params))
                .timeout(10 * 60 * 1000)
                .execute().body();
    }

    /**
     * 测试推送消息
     *
     * @param token
     * @return
     */
    private static String sendMsg(String token) {
        Map<String, List<String>> headers = new HashMap<>();
        List<String> appidlist = new ArrayList<>();
        appidlist.add(APPID);
        headers.put("appid", appidlist);
        List<String> tokenlist = new ArrayList<>();
        tokenlist.add(token);
        headers.put("token", tokenlist);
        List<String> skipsessionlist = new ArrayList<>();
        skipsessionlist.add("1");
        headers.put("skipsession", skipsessionlist);
        Map<String, String> msginfo = new HashMap<>();
        msginfo.put("code", code);
        msginfo.put("userIdList", "1");
        msginfo.put("creater", "1");
        msginfo.put("title", "OA测试广联达消息提醒01");
        msginfo.put("context", "这是消息内容01");
        msginfo.put("linkUrl", "https://www.baidu.com");
        msginfo.put("linkMobileUrl", "https://m.baidu.com");
        return post("/api/ec/dev/message/sendCustomMessageSingle", headers, msginfo);
    }

    /**
     * 将Map转换成字符串参数，用于POST  请求
     *
     * @param map
     * @return
     */
    public static String mapToStr(Map<String, String> map) {
        StringBuilder stringBuilder = new StringBuilder();
        if (map != null) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                stringBuilder.append(entry.getKey());
                if (entry.getValue() != null)
                    stringBuilder.append("=").append(entry.getValue());
                stringBuilder.append("&");
            }
        }
        if (stringBuilder.length() > 0)
            return stringBuilder.substring(0, stringBuilder.length() - 1);
        return null;
    }
}
